package com.pickgliss.ui.controls.container
{
	import flash.display.DisplayObject;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	
	public class SimpleTileList extends BoxContainer
	{
		public var startPos:Point = new Point(0,0);
		
		public function SimpleTileList(columnNum:int=1,arrangeType:int = 0)
		{
			super();
			_column = columnNum;
			_arrangeType = arrangeType;
		}
		
		protected var _column:int;
		protected var _arrangeType:int;
		protected var _hSpace:Number = 0;
		protected var _rowNum:int;
		protected var _vSpace:Number = 0;
		private var _selectedIndex:int;
		
		public function get selectedIndex():int
		{
			return _selectedIndex;
		}
		
		public function set selectedIndex(value:int):void
		{
			if(_selectedIndex==value)return;
			_selectedIndex = value;
		}
		
		public function get hSpace():Number
		{
			return _hSpace;
		}
		
		public function set hSpace(value:Number):void
		{
			_hSpace = value;
			onProppertiesUpdate();
		}
		
		public function get vSpace():Number
		{
			return _vSpace;
		}
		
		public function set vSpace(value:Number):void
		{
			_vSpace = value;
			onProppertiesUpdate();
		}
		
		override public function addChild(child:DisplayObject):DisplayObject{
			child.addEventListener(MouseEvent.CLICK,__itemClick);
			super.addChild(child);
			return child;
		}
		
		private function __itemClick(e:MouseEvent):void{
			var item:DisplayObject = e.currentTarget as DisplayObject;
			_selectedIndex = getChildIndex(item);
		}
		
		override protected function arrange():void
		{
			caculateRows();
			if(_arrangeType == 0)
			{
				horizontalArrange();
			}else
			{
				verticalArrange();
			}
		}
		
		private function horizontalArrange():void
		{
			var n:int = 0;
			var posX:int = startPos.x;
			var posY:int = startPos.y;
			var tempWidth:int = 0;
			var tempHeight:int = 0;
			for(var i:int = 0; i<_rowNum; i++)
			{
				var maxHeight:int = 0;
				for(var j:int = 0; j<_column; j++)
				{
					var ch:DisplayObject = getChildAt(n++);
					ch.x = posX;
					ch.y = posY;
					tempWidth = Math.max(tempWidth,posX + ch.width);
					tempHeight = Math.max(tempHeight,posY+ch.height);
					posX += (ch.width + _hSpace);
					if(maxHeight < ch.height)
					{
						maxHeight = ch.height;
					}
					if(n>=numChildren)
					{
						changeSize(tempWidth,tempHeight);
						return;
					}
				}
				posX = startPos.x;
				posY += (maxHeight + _vSpace);
			}
			changeSize(tempWidth,tempHeight);
			dispatchEvent(new Event(Event.RESIZE));
		}
		
		private function verticalArrange():void
		{
			var n:int = 0;
			var posX:int = startPos.x;
			var posY:int = startPos.y;
			var tempWidth:int = 0;
			var tempHeight:int = 0;
			for(var i:int = 0; i<_rowNum; i++)
			{
				var maxWidth:int = 0;
				for(var j:int = 0; j<_column; j++)
				{
					var ch:DisplayObject = getChildAt(n++);
					ch.x = posX;
					ch.y = posY;
					tempWidth = Math.max(tempWidth,posX + ch.width);
					tempHeight = Math.max(tempHeight,posY+ch.height);
					posY += (ch.height + _vSpace);
					if(maxWidth < ch.width)
					{
						maxWidth = ch.width;
					}
					if(n>=numChildren)
					{
						changeSize(tempWidth,tempHeight);
						return;
					}
				}
				posX += (maxWidth + _hSpace);
				posY = startPos.y;
			}
			changeSize(tempWidth,tempHeight);
			dispatchEvent(new Event(Event.RESIZE));
		}
		
		private function changeSize(w:int,h:int):void
		{
			if(w != _width || h != _height)
			{
				width = w;
				height = h;
			}
		}
		
		private function caculateRows():void
		{
			_rowNum = Math.ceil(numChildren/_column);
		}
		
		override public function dispose():void{
			for (var i:int = 0; i < numChildren; i++)
			{
				var item:DisplayObject = getChildAt(i) as DisplayObject
				item.removeEventListener(MouseEvent.CLICK,__itemClick);
			}
			super.dispose();
		}
	}
}